/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package pe.edu.pucp.dp1.lectura.controlador;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import javax.swing.JFileChooser;

/**
 *
 * @author Ed
 */
public class VentanaDatos extends javax.swing.JFrame {
    
    GestorCar gestorCar = GestorCar.getInstance();
    File f;
    boolean approve_option;
    
    

    /**
     * Creates new form VentanaDatos
     */
    public VentanaDatos() {
        initComponents();
        this.setVisible(true);
        //centrar ini
        //centrar ini
        //centrar ini
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        // Determine the new location of the window
        int w = this.getSize().width;
        int h = this.getSize().height;
        int x = (dim.width-w)/2;
        int y = (dim.height-h)/2;
        // Move the window
        this.setLocation(x, y);
        //centrar fin
        //centrar fin
        //centrar fin
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        ipServProcPanel = new javax.swing.JPanel();
        ipServProcText = new javax.swing.JTextField();
        ipServProcLabel = new javax.swing.JLabel();
        archivosGPSPanel = new javax.swing.JPanel();
        rutaCarpetaText = new javax.swing.JTextField();
        buscarCarpetaButton = new javax.swing.JButton();
        runLecturaButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        ipServProcPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Servidor Procesamiento"));
        ipServProcPanel.setName("ipServProcPanel"); // NOI18N

        ipServProcText.setFocusable(false);
        ipServProcText.setName("ipServProcText"); // NOI18N

        ipServProcLabel.setText("IP");
        ipServProcLabel.setName("ipServProcLabel"); // NOI18N

        javax.swing.GroupLayout ipServProcPanelLayout = new javax.swing.GroupLayout(ipServProcPanel);
        ipServProcPanel.setLayout(ipServProcPanelLayout);
        ipServProcPanelLayout.setHorizontalGroup(
            ipServProcPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, ipServProcPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(ipServProcLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 120, Short.MAX_VALUE)
                .addComponent(ipServProcText, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(20, 20, 20))
        );
        ipServProcPanelLayout.setVerticalGroup(
            ipServProcPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ipServProcPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(ipServProcPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(ipServProcLabel)
                    .addComponent(ipServProcText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        archivosGPSPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Archivos GPS"));
        archivosGPSPanel.setName("archivosGPSPanel"); // NOI18N

        rutaCarpetaText.setName("rutaCarpetaText"); // NOI18N

        buscarCarpetaButton.setText("Buscar");
        buscarCarpetaButton.setName("buscarCarpetaButton"); // NOI18N
        buscarCarpetaButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buscarCarpetaButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout archivosGPSPanelLayout = new javax.swing.GroupLayout(archivosGPSPanel);
        archivosGPSPanel.setLayout(archivosGPSPanelLayout);
        archivosGPSPanelLayout.setHorizontalGroup(
            archivosGPSPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(archivosGPSPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(archivosGPSPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(rutaCarpetaText, javax.swing.GroupLayout.PREFERRED_SIZE, 324, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buscarCarpetaButton, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(18, Short.MAX_VALUE))
        );
        archivosGPSPanelLayout.setVerticalGroup(
            archivosGPSPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(archivosGPSPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(rutaCarpetaText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buscarCarpetaButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        runLecturaButton.setText("Run");
        runLecturaButton.setName("runLecturaButton"); // NOI18N
        runLecturaButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                runLecturaButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(archivosGPSPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(ipServProcPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(runLecturaButton)
                .addGap(176, 176, 176))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(ipServProcPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(archivosGPSPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(runLecturaButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void runLecturaButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_runLecturaButtonActionPerformed
        // TODO add your handling code here:
        if(approve_option){
            gestorCar.setRuta(f);            
            gestorCar.setIpServProcesamiento(ipServProcText.getText());
            this.dispose();        
        }        
    }//GEN-LAST:event_runLecturaButtonActionPerformed

    private void buscarCarpetaButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buscarCarpetaButtonActionPerformed
        // TODO add your handling code here:
        JFileChooser selector = new JFileChooser();
        selector.setCurrentDirectory(new java.io.File("."));
        selector.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        selector.setAcceptAllFileFilterUsed(false);
        if(selector.showOpenDialog(this)==JFileChooser.APPROVE_OPTION)
        {
            this.approve_option=true;
            rutaCarpetaText.setText(selector.getSelectedFile().getAbsolutePath());
            f = selector.getSelectedFile();
        }
    }//GEN-LAST:event_buscarCarpetaButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(VentanaDatos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(VentanaDatos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(VentanaDatos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(VentanaDatos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */        
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new VentanaDatos().setVisible(true);                
            }
        });        
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel archivosGPSPanel;
    private javax.swing.JButton buscarCarpetaButton;
    private javax.swing.JLabel ipServProcLabel;
    private javax.swing.JPanel ipServProcPanel;
    private javax.swing.JTextField ipServProcText;
    private javax.swing.JButton runLecturaButton;
    private javax.swing.JTextField rutaCarpetaText;
    // End of variables declaration//GEN-END:variables

}
